﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DNA.Mvc.Services;
using System.Web.Security;
using DNA.Mvc.Notification;

namespace DNA.Mvc.Controllers
{
    public class SyndicationController : Controller
    {
        private WebSiteContext _context;
        
        public SyndicationController(WebSiteContext context)
        {
            _context = context;
        }
       
        [Authorize,HttpPost]
        public bool Subscribe(string token)
        {
            if (string.IsNullOrEmpty(token))
                return false;

            var userName = User.Identity.Name;
            var decodeToken = Server.UrlDecode(token);

            bool isSubscribed = Notifier.IsSubscribed(decodeToken, userName);

            if (isSubscribed)
                Notifier.Unsubscribe(decodeToken, userName);
            else
                Notifier.Subscribe(decodeToken, userName);
            return !isSubscribed;
        }

        [OutputCache(Duration = 36000, VaryByParam = "*")]
        public ActionResult RssFeed()
        {
            var rssGenerator = new RssDocumentGenerator(DependencyResolver.Current.GetServices<RssItemsGeneratorBase>());
            var web=WebSiteContext.Current.Web;
            rssGenerator.SetTitle(web.Title)
                .SetDesc(web.Description)
                .SetCopyright(web.Copyright)
                .SetGenerator("DotNetAge" + DnaConfig.RuntimeVersion)
                .SetLanguage(web.DefaultLanguage)
                .SetLink(UrlResolver.Resolve(_context.HttpContext, web))
                .SetWebmasterEmail(Membership.GetUser(web.Owner).Email + " (" + web.Owner + ")")
                .SetPubDate(web.Created.ToUniversalTime());
            
            if (!string.IsNullOrEmpty(web.LogoImageUrl))
                rssGenerator.SetImage(_context.ApplicationPath + VirtualPathUtility.ToAbsolute(web.LogoImageUrl), web.Title, web.Description);

            return Content(rssGenerator.GetXml(), "text/xml");
        }

        [OutputCache(Duration = 36000, VaryByParam = "*")]
        public ActionResult AtomFeed()
        {
            var atomGenerator = new AtomFeedGenerator(DependencyResolver.Current.GetServices<AtomEntriesGeneratorBase>());
            var web=WebSiteContext.Current.Web;
            atomGenerator.SetBaseInfo(web.Title, id: UrlResolver.Resolve(_context.HttpContext,web), copyright: web.Copyright)
                .SetIcon(web.ShortcutIconUrl)
                .SetLogo(web.LogoImageUrl)
                .SetGenerator(UrlResolver.Resolve(_context.HttpContext,web), System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), "DotNetAge");

            return Content(atomGenerator.GetXml(), "text/xml");
        }

        [OutputCache(Duration = 36000, VaryByParam = "*")]
        public ActionResult Sitemap()
        {
            var sitemap = new SiteMapIndexGenerator(DependencyResolver.Current.GetServices<SiteMapGeneratorBase>());
            return Content(sitemap.GetXml(), "text/xml");
        }

        [OutputCache(Duration = 36000, VaryByParam = "*")]
        public ActionResult Opml()
        {
            var opml = new OpmlDocumentGenerator(DependencyResolver.Current.GetServices<OpmlOutlineGeneratorBase>());
             var web=WebSiteContext.Current.Web;

             opml.SetTitle(web.Title)
                 .SetOwner(web.Owner, web.Owner, Membership.GetUser(web.Owner).Email);
                
            return Content(opml.GetXml(), "text/xml");
        }
    }
}
